<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Login extends Controller
{
	
	public function index()
	{
		if (Request::instance()->isPost()) 
		{
			$username = Request::instance()->post('username');
			$password = Request::instance()->post('password');
			if(empty($username)){
				$this->error('用户名不能为空');
				exit();
			}if(empty($password)){
				$this->error('密码不能为空');
				exit();
			}
			$where['username'] = $username; 
			$where['password'] = msc($password);
			$where['status']   = 1;
			$res = DB::name('user')->where($where)->find();
			if($res){
				Session::set('manage_id',$res['id']);
				$this->success('登录成功', 'index/index/index');
			}else{
				$this->error('登录失败');
			}
		}
		/**
         * 检测用户是否登录
         */
        if (true === Session::has('manage_id')) {
            $this->redirect('index/index/index');
        }

        return $this->fetch();
	}


	public function logout()
	{
        Session::delete('manage_id');
        $this->redirect('index/login/index');
    }

    public function LockScreen()
    {
 		if (true === Session::has('manage_id')) {
 			$uid = Session::get('manage_id');
	    	// $pic = DB::name('user')->field('avatar')->where('status = 1 and id = '.$uid)->find();
	    	if(Request::instance()->isPost()){
	    		$password = Request::instance()->post('password');
	    		$where['id'] = $uid;
	    		$where['password'] = msc($password);
	    		$where['status'] = 1;
	    		$res = DB::name('user')->where($where)->find();
	    		if($res){
	    			$this->success('解锁成功','index/index/index');
	    		}else{
	    			$this->error('密码错误');
	    		}
	    	}
	    	return $this->fetch('lockScreen');
    	}else{
    		$this->error('系统出现问题，请重新登录');
    	}
    }
}
?>